package edu.gyc.iotest;

import java.io.*;



public class MyDirectory {
    //复制文件 src源文件路径，dest目的文件路径,应输出复制文件所耗费时间 ，可使用
    // long start=System.currentTimeMillis(); 获取当前时间的毫秒值
    public static void copyFile(String src,String dest){
        long start=System.currentTimeMillis();
        InputStream in =null;
        OutputStream out =null;
        try {
            in=new FileInputStream(src);
            out=new FileOutputStream(dest);
            byte[] b=new byte[4*1024];
            int n =0;
            while ((n=in.read(b))!=-1) {
                out.write(b, 0, n);
            }

            System.out.println("Copy File "+src+" to "+dest+" expire "+(System.currentTimeMillis()-start)+"ms");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
          try {
              in.close();
              out.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
        }

    }
   //复制目录 src下的所有子目录和文件都必须复制，目录结构不能改变
    public static boolean copyDirectory(String src,String dest){
        File srcFile=new File(src);
        File destFile=new File(dest);

        if(srcFile.isFile()||destFile.isFile()){
            System.out.println("源文件夹或目的文件夹路径错误");
            return false;
        }

        destFile.mkdirs();
        File[] files=srcFile.listFiles();
        for (File f: files) {
            if(f.isFile()){
                copyFile(f.getAbsolutePath(),dest+File.separator+f.getName());

            }else{
                copyDirectory(f.getAbsolutePath(),dest+File.separator+f.getName());
            }
        }
        return true;
    }
   //删除指定目录及其子目录和文件
    public static boolean deleteDirectory(String src){
        File file=new File(src);
        if(file.isFile()){
            file.delete();
            return true;
        }

        File[] files=file.listFiles();
        for(File f: files){
            if(f.isFile())
                f.delete();
            else {
                deleteDirectory(f.getAbsolutePath());
            }
        }
        file.delete();
        System.out.println("Delete "+src+" success.");
        return true;
    }
    //移动目录
    public static  void  moveDirectory(String src,String dest){
        
        //copyDirectory(src, dest);
        //deleteDirectory(src);
        copyDirectory(src, dest);
        deleteDirectory(src);
       // JOptionPane.showInputDialog("Hello");
        System.out.println("Move "+src+" to "+dest+" success.");

    }
    //遍历指定目录及其子目录所有文件，打印出来
       public static void traverseDir(String src){
          
       File file =new File(src);
       if (file.isFile()) {
           System.out.println("源路径是文件无须遍历目录。");
           return;
       }
       File[] files=file.listFiles();
       for (File file1 : files) {
           if (file1.isFile()) {
               System.out.println(""+file1.getAbsolutePath());
           }else if (file1.isDirectory()) {
               traverseDir(file1.getAbsolutePath());
           }
       }
       return;
   }

   //逐一测试每一个静态方法
    public static void main(String[] args) {
     // copyFile("d:/tea/001.jpg","d:/1.jpg");
       copyDirectory("d:/tea","d:/c/d");
     //deleteDirectory("d:/c/d");
      // moveDirectory("d:/c/d","d:/d");
       // traverseDir("d:/d");
    }
}
